package com.sz.youban.controller.terminal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.feilong.core.Validator;
import com.sz.youban.controller.base.BaseController;
import com.sz.youban.dt.entity.result.ResultModel;
import com.sz.youban.dt.entity.terminal.DtCallRecords;
import com.sz.youban.dt.entity.terminal.DtMsgRecords;
import com.sz.youban.dt.entity.terminal.DtTelList;
import com.sz.youban.dt.interfs.terminal.IDtCallRecordsService;
import com.sz.youban.dt.interfs.terminal.IDtMsgRecordsService;
import com.sz.youban.dt.interfs.terminal.IDtTelListService;
import com.sz.youban.dt.model.CallRecord;
import com.sz.youban.dt.model.LinkUser;
import com.sz.youban.dt.model.MsgRecord;
import com.sz.youban.dt.model.QueryRecordTimeRequest;
import com.sz.youban.dt.model.QueryRecordTimeResponse;
import com.sz.youban.dt.model.SaveCallRecordsRequest;
import com.sz.youban.dt.model.SaveMsgRecordsRequest;
import com.sz.youban.dt.model.SaveTelListRequest;
import com.sz.youban.dt.service.utils.DateUtil;


@RestController
@RequestMapping("/terminal")
public class TerminalController extends BaseController{

	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IDtTelListService telListService;

	@Autowired
	private IDtCallRecordsService callRecordsService;
	
	@Autowired
	private IDtMsgRecordsService msgRecordsService;
	
	/**
	 * 存储通讯录信息
	 * @return
	 */
	@PostMapping(value="saveTelList",consumes="application/json")
	public ResultModel saveTelList(@RequestBody SaveTelListRequest saveTelListRequest){
		
		log.info("----- saveTelList 接口请求参数 -----{}" , saveTelListRequest.toString());

		//校验参数
		if(Validator.isNullOrEmpty(saveTelListRequest) || Validator.isNullOrEmpty(saveTelListRequest.getUserId()) || Validator.isNullOrEmpty(saveTelListRequest.getLinkUserList())){

			//必要参数不足，返回失败
			return errorParam();
		}

		List<LinkUser> linkUserList = saveTelListRequest.getLinkUserList();

		DtTelList telList = new DtTelList();
		telList.setUserId(saveTelListRequest.getUserId());
		telList.setAppPhone(saveTelListRequest.getAppPhone());
		telList.setChannel(saveTelListRequest.getChanel());

		for (LinkUser linkUser : linkUserList) {
			telList.setLinkMan(linkUser.getLinkMan());

			String[] phones=linkUser.getPhone().split(",");
			for (int i = 0; i < phones.length; i++) {

				//组装数据
				telList.setPhone(phones[i]);

				//存入db
				int result = telListService.saveTelList(telList);
			}
		}

		return success();
	}


	/**
	 * 存储通话记录
	 * @return
	 */
	@PostMapping(value="saveCallRecords",consumes="application/json")
	public ResultModel saveCallRecords(@RequestBody SaveCallRecordsRequest saveCallRecordsRequest){
		
		//校验参数
		if(Validator.isNullOrEmpty(saveCallRecordsRequest) || Validator.isNullOrEmpty(saveCallRecordsRequest.getUserId()) || Validator.isNullOrEmpty(saveCallRecordsRequest.getCallRecordList())){

			//必要参数不足，返回失败
			return errorParam();
		}

		List<CallRecord> callRecordList = saveCallRecordsRequest.getCallRecordList();

		DtCallRecords callRecords = new DtCallRecords();
		callRecords.setUserId(saveCallRecordsRequest.getUserId());
		callRecords.setAppPhone(saveCallRecordsRequest.getAppPhone());
		
		log.info("----- saveCallRecords 接口请求参数 -----{}" , saveCallRecordsRequest.toString());
		
		for(CallRecord callRecord : callRecordList){
			
			//组装数据
			callRecords.setPhone(callRecord.getPhone());			
			callRecords.setStartTime(DateUtil.str2Date(callRecord.getStartTime() + "" , "yyyy-MM-dd HH:mm:ss"));
			callRecords.setSustainTime(callRecord.getSustainTime());
			callRecords.setCallFlag(callRecord.getCallFlag());
			
			//存入db
			int result = callRecordsService.saveCallRecords(callRecords);
		}
		
		return success();
	}
	
	
	/**
	 * 存储短信记录
	 * @return
	 */
	@PostMapping(value="saveMsgRecords",consumes="application/json")
	public ResultModel saveMsgRecords(@RequestBody SaveMsgRecordsRequest saveMsgRecordsRequest){
		
		log.info("----- saveMsgRecords 接口请求参数 -----{}" , saveMsgRecordsRequest.toString());
		
		//校验参数
		if(Validator.isNullOrEmpty(saveMsgRecordsRequest) || Validator.isNullOrEmpty(saveMsgRecordsRequest.getUserId()) || Validator.isNullOrEmpty(saveMsgRecordsRequest.getMsgRecordList())){

			//必要参数不足，返回失败
			return errorParam();	
		}

		List<MsgRecord> msgRecordList = saveMsgRecordsRequest.getMsgRecordList();
		
		DtMsgRecords msgRecords = new DtMsgRecords();
		msgRecords.setUserId(saveMsgRecordsRequest.getUserId());
		msgRecords.setAppPhone(saveMsgRecordsRequest.getAppPhone());
		
		for(MsgRecord msgRecord : msgRecordList){
			
			//组装数据
			msgRecords.setPhone(msgRecord.getPhone());
			msgRecords.setTransferTime(DateUtil.str2Date(msgRecord.getTransferTime() + "" , "yyyy-MM-dd HH:mm:ss"));
			msgRecords.setContent(msgRecord.getContent());
			msgRecords.setCallFlag(msgRecord.getCallFlag());

			//存入db
			int result = msgRecordsService.saveCallRecords(msgRecords);
		}
		
		return success();
	}

	/**
	 * 查询记录上次存储时间
	 * type： 1：通讯录 ，2：通话记录，3：短信记录
	 * @return
	 */
	@PostMapping(value="queryRecordTime",consumes="application/json")
	public ResultModel queryRecordTime(@RequestBody QueryRecordTimeRequest queryRecordTimeRequest){

		log.info("----- queryRecordTime 接口请求参数 -----{}" , queryRecordTimeRequest.toString());

		//校验参数
		if(Validator.isNullOrEmpty(queryRecordTimeRequest) || Validator.isNullOrEmpty(queryRecordTimeRequest.getAppPhone()) || Validator.isNullOrEmpty(queryRecordTimeRequest.getType())){
			return errorParam();	//必要参数不足，返回失败
		}if ( queryRecordTimeRequest.getType() > 3 || queryRecordTimeRequest.getType() < 1){
			return errorParam();
		}

		//获取请求参数
		String appPhone = queryRecordTimeRequest.getAppPhone();
		int type = queryRecordTimeRequest.getType();

		//查询结果并返回
		QueryRecordTimeResponse response = new QueryRecordTimeResponse();

		Map<String,String> map = new HashMap<String,String>();
		if(type == 1){
			map = telListService.selectRecordTime(appPhone);
		}else if(type == 2){
			map = callRecordsService.selectRecordTime(appPhone);
		}else if(type == 3){
			map = msgRecordsService.selectRecordTime(appPhone);
		}

		response.setExist(map.get("exist"));
		response.setLastTime(map.get("lastTime"));
		return success(response);
	}

}
